package com.ocom.wxapi.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.dto.AreaDto;
import com.ocom.common.request.web.CommonPidVo;
import com.ocom.common.request.wx.AccessPageNewAreaRequest;
import com.ocom.common.request.wx.AccessPageRemoveAreaRequest;
import com.ocom.common.request.wx.AccessPageupdateNameRequest;
import com.ocom.common.request.wx.CommonRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.wx.AccessPageOnShowResponse;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.AreaInfoVo;
import com.ocom.wxapi.client.AccessClientService;
import com.ocom.wxapi.client.MjRecodeClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@PreAuthorize("hasRole('ROLE_WX')")
@RequestMapping("/AccessPage")
@RestController
public class AccessPageController {

    @Autowired
    AccessClientService accessClientService;

    @Autowired
    MjRecodeClientService mjRecodeClientService;

    @PostMapping("/onShow")
    public Result onShow(@Valid CommonRequest request){

        AccessPageOnShowResponse accessPageOnShowResponse=new AccessPageOnShowResponse();

        Map<String,String> map =new HashMap<>();
        map.put("pageTitle","门禁记录+");
        map.put("lblUserCaption","允许开门人数：");
        map.put("lblMachCaption","绑定设备数：");
        map.put("cmdNameCaption","修改名称");
        map.put("cmdAccessCaption","人员权限");
        map.put("cmdMachCaption","添加设备");
        map.put("cmdRecordCaption","查询开门记录");
        map.put("cmdRemoveCaption","修改名称");
        map.put("cmdAddCaption","+添加区域");
        accessPageOnShowResponse.setPageFormat(map);


        Result result=accessClientService.getAreaList(request.getCompanyId(),request.getPId());
        List<AreaInfoVo> list= JSON.parseObject(JSON.toJSONString(result.getData()), List.class);
        accessPageOnShowResponse.setAreaInfo(list);

        return ResultUtil.success(accessPageOnShowResponse);
    }


    @PostMapping("/updateName")
    public Result updateName(@Valid AccessPageupdateNameRequest request){
        AreaDto areaDto=new AreaDto();
        areaDto.setAreaName(request.getAreaName());
        areaDto.setComId(request.getCompanyId());
        areaDto.setAreaId(request.getAreaNo());
        Result result=accessClientService.UpdateAreaName(areaDto);
        if(result.getCode()!=200){
            return ResultUtil.info(result.getCode(),result.getMsg());
        }
        Map<String,String> map=new HashMap<>();
        map.put("newName",request.getAreaName());
        return ResultUtil.info(200,"区域名称已修改。",map);
    }



    @PostMapping("/removeArea")
    public Result removeArea(@Valid AccessPageRemoveAreaRequest request){
        AreaDto areaDto=new AreaDto();
        areaDto.setComId(request.getCompanyId());
        areaDto.setPId(request.getPId());
        areaDto.setAreaId(request.getAreaNo());
        Result result=accessClientService.RemoveArea(areaDto);
        if(result.getCode()!=200){
            return ResultUtil.info(result.getCode(),result.getMsg());
        }
        return ResultUtil.info(200,"删除成功。");
    }



    @PostMapping("/newArea")
    public Result newArea(@Valid AccessPageNewAreaRequest request){
        Result result=accessClientService.newArea(request.getCompanyId(),request.getRegionId(),request.getAreaName(),request.getPId());
        if(result.getCode()==200){
            Result areaList = accessClientService.getAreaList(request.getCompanyId(), request.getPId());
            return ResultUtil.info(200,"已成功添加区域:"+request.getAreaName(),areaList.getData());
        }else{
            return ResultUtil.error(result.getCode());
        }

    }

    @PostMapping("/areas")
    public Result areas(@Valid CommonPidVo request){
        return accessClientService.GetAreaInfo(request.getComId());
    }




}
